package com.assessment.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;

@Entity
@Table(name="t_assessment_project")
public class AssessmentProject 
{
	private Long id;
	private String name;
	//private Staff founder;  //创建人
	private AssessmentTemplate assessmentTemplate;
	private Date starTime;
	private Date endTime;
	//private List<Staff> inspectionObjectList=new ArrayList();
	private Staff superior;  //负责人

	
	//临时变量
	private String examine;
	private Long superiorId;  //负责人
	private String superiorName;  //负责人
	private Long assessmentTemplateId;
	private String assessmentTemplateName;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	public String getName() {
		return name;
	}
//	@ManyToOne(cascade=CascadeType.ALL)
//	public Staff getFounder() {
//		return founder;
//	}
	@ManyToOne(cascade=CascadeType.ALL)
	public AssessmentTemplate getAssessmentTemplate() {
		return assessmentTemplate;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getStarTime() {
		return starTime;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getEndTime() {
		return endTime;
	}
	
    /*	
    @ManyToMany(cascade=CascadeType.ALL)
	public List<Staff> getInspectionObjectList() {
		return inspectionObjectList;
	}
    */	
	@ManyToOne(cascade=CascadeType.ALL)
	public Staff getSuperior() {
		return superior;
	}
	@Transient
	public String getExamine() {
		return examine;
	}
	@Transient
	public Long getSuperiorId() {
		return superiorId;
	}
	@Transient
	public String getSuperiorName() {
		return superiorName;
	}
	@Transient	
	public Long getAssessmentTemplateId() {
		return assessmentTemplateId;
	}
	@Transient
	public String getAssessmentTemplateName() {
		return assessmentTemplateName;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
//	public void setFounder(Staff founder) {
//		this.founder = founder;
//	}
	public void setAssessmentTemplate(AssessmentTemplate assessmentTemplate) {
		this.assessmentTemplate = assessmentTemplate;
	}
	public void setStarTime(Date starTime) {
		this.starTime = starTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	/*	
	public void setInspectionObjectList(List<Staff> inspectionObjectList) {
		this.inspectionObjectList = inspectionObjectList;
	}		
	*/
	public void setSuperior(Staff superior) {
		this.superior = superior;
	}
	public void setExamine(String examine) {
		this.examine = examine;
	}
		
	public void setSuperiorId(Long superiorId) {
		this.superiorId = superiorId;
	}
	public void setSuperiorName(String superiorName) {
		this.superiorName = superiorName;
	}	
	public void setAssessmentTemplateId(Long assessmentTemplateId) {
		this.assessmentTemplateId = assessmentTemplateId;
	}
	public void setAssessmentTemplateName(String assessmentTemplateName) {
		this.assessmentTemplateName = assessmentTemplateName;
	}
	public static Specification<AssessmentProject> getSpecification( final AssessmentProject assessmentProject)
	{
		Specification<AssessmentProject> spec = new Specification<AssessmentProject>() {
			public Predicate toPredicate(Root<AssessmentProject> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != assessmentProject && !StringUtils.isEmpty(assessmentProject.getId())) {
					 Predicate  p1 =  cb.equal(root.get("id").as(String.class),assessmentProject.getId());
					 list.add(p1);
				 }
				 if(null != assessmentProject && !StringUtils.isEmpty(assessmentProject.getName())) {
					 Predicate  p2 =  cb.like(root.get("name").as(String.class),"%"+ assessmentProject.getName()+ "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
	
}
